<?php
/**
 * 股东系统
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/10/25
 * Time: 11:24
 */
namespace app\admin\controller;

use app\common\model\McMembers;
use app\common\model\Shareholders;
use app\common\model\EweiShopMember;
use app\common\model\ShareholdersMoneyLog;
use app\common\model\ShareholdersSharesLog;
use think\Db;
use think\log;
use think\Loader;

class Team extends Base
{

    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
    }

    public function teamlist()
    {
        $phone = input('get.phone');
        $mM = model('EweiShopMember');
        $sprM = model('ShareholdersPerformanceRLog');
        $sM = model('Shareholders');
        $page = intval(input('request.page', 0)) < 1 ? 1 : intval(input('request.page', 0));
        $page_data['page'] = $page;
        $page_data['size'] = 10;
        if ($phone) {
            $s = $sM->where(['phone' => $phone])->find();
            $sum = $sprM->where(['sid' => $s['id']])->sum('price');
            $team['total'] = $sum;
            $team['sid'] = $s['id'];
            $team['username'] = $s->User->nickname;
            $recommend = $mM -> where(['parent_id' => $s['uid']])-> field('nickname') -> select();
            $str = [];
            foreach($recommend as $r){
                $str[] = $r['nickname'];
            }
            $team['recommend'] = implode(',',$str);
            $teamList[] = $team;
            $this->assign('teamList', $teamList);
            $this->assign('error', '');
            return $this->fetch();
        } else {
            $page_data['total'] = $sprM->group('sid')->count();
            $page_html = $this->getPageHtml($page_data);
            $sql = "select sid,SUM(price) as total from ims_shareholders_performance_r_log GROUP BY sid ORDER BY total DESC LIMIT ".($page_data['page'] *$page_data['size']).",".$page_data['size'] ;
            $all = Db::query($sql);
            $teamList = [];
            foreach($all as $u){
                $s = $sM -> where(['id' => $u['sid']]) -> find();
                $u['username'] = $s -> User -> nickname;
                $recommend = $mM -> where(['parent_id' => $s['uid']])-> field('nickname') -> select();
                $str = [];
                foreach($recommend as $r){
                    $str[] = $r['nickname'];
                }
                $u['recommend'] = implode(',',$str);
                $teamList[] = $u;
            }
            $this->assign('teamList', $teamList);
            $this->assign('page_html', $page_html);
            $this->assign('error', '');
            return $this->fetch();
        }
    }

    public function fteam()
    {
        $sid = input('get.sid');
        if ($sid) {
            $mM = model('EweiShopMember');
            $sM = model('Shareholders');
            $sprM = model('ShareholdersPerformanceRLog');
            $s = $sM->where(['id' => $sid])->find();
            $userTeam['user'] = $s;
            $userT = $mM->where(['parent_id' => $s['uid']])->select();
            foreach($userT as $key => $value){
                $us = $sM -> where(['uid' => $value['id']]) -> find();
                $userT[$key]['sid'] = $us['id'];

                $sum = $sprM->where(['sid' => $us['id']])->sum('price');
                $userT[$key]['total'] = $sum;

                $recommend = $mM -> where(['parent_id' => $us['uid']])-> field('nickname') -> select();
                $str = [];
                foreach($recommend as $r){
                    $str[] = $r['nickname'];
                }
                $userT[$key]['recommend'] = implode(',',$str);
            }
            $userTeam['userTeam'] = $userT;
            $this->assign('userTeam', $userTeam);
            $this->assign('sid', $sid);
            $this->assign('error', '');
            return $this->fetch();
        } else {
            $this->assign('userTeam', '');
            $this->assign('error', '请输入正确的手机号');
            return $this->fetch();
        }
    }
    public function saveExcel()
    {
        $mM = model('EweiShopMember');
        $sprM = model('ShareholdersPerformanceRLog');
        $sM = model('Shareholders');
        $sql = "select sid,SUM(price) as total from ims_shareholders_performance_r_log GROUP BY sid ORDER BY total DESC ";
        $all = Db::query($sql);
        $teamList = [];
        foreach ($all as $u) {
            $s = $sM->where(['id' => $u['sid']])->find();
            $u['username'] = $s->User->nickname;
            $recommend = $mM->where(['parent_id' => $s['uid']])->field('nickname')->select();
            $str = [];
            foreach ($recommend as $r) {
                $str[] = $r['nickname'];
            }
            $u['recommend'] = implode(',', $str);
            $teamList[] = $u;
        }
        include_once('./phpexcel/phpexcel_function.php');
        outToExcel($teamList);
    }

    public function saveExcel1()
    {
        $sid = input('get.sid');
        $mM = model('EweiShopMember');
        $sM = model('Shareholders');
        $sprM = model('ShareholdersPerformanceRLog');
        $s = $sM->where(['id' => $sid])->find();
        $userTeam['user'] = $s;
        $userT = $mM->where(['parent_id' => $s['uid']])->select();
        foreach($userT as $key => $value){
            $us = $sM -> where(['uid' => $value['id']]) -> find();
            $userT[$key]['sid'] = $us['id'];

            $sum = $sprM->where(['sid' => $us['id']])->sum('price');
            $userT[$key]['total'] = $sum;

            $recommend = $mM -> where(['parent_id' => $us['uid']])-> field('nickname') -> select();
            $str = [];
            foreach($recommend as $r){
                $str[] = $r['nickname'];
            }
            $userT[$key]['recommend'] = implode(',',$str);
        }
        $userTeam[] = $userT;
        $array = json_decode( json_encode( $userTeam),true);
        unset($array['user']);
        $array = $array[0];
        foreach ($array as $value){
            $newArray['sid']=$value['sid'];
            $newArray['username']=$value['nickname'];
            $newArray['total']=$value['total'];
            $newArray['recommend']=$value['recommend'];
            $newArray2[]=$newArray;
        }
        /*echo "<pre>";
        print_r($newArray2);die;*/
        include_once('./phpexcel/phpexcel_function.php');
        outToExcel($newArray2);
    }
}